<?php
# 文件名称:document.php
# MetInfo在线反馈系统 
# Copyright (C) 长沙米拓信息技术有限公司 (http://www.metinfo.cn). All rights reserved.
defined( '_SYS' ) or die( 'No direct script access allowed' );
class C_Document
{
	var $title = '';
	var $description = '';
	var $link = '';
	var $language = '';
	
	var $_keywords = '';
	var $_generator = '';
	
	var $_charset = 'utf-8';
	var $_mime = '';
	var $_scripts = array();
	var $_script = array();
	var $_styleSheets = array();
	var $_style = array();
	var $_metaTags = array();
	var $_type = null;

	var $_mdate = null;
	
	var $_links = array();

	var $_custom = array();
	function C_Document()
	{
		//set document type
		$this->_type = 'html';
		//set mime type
		$this->_mime = 'text/html';
		//set default document metadata
		 $this->setMetaData('Content-Type', $this->_mime . '; charset=' . $this->_charset , true );
		 $this->setMetaData('robots', 'index, follow' );
	}
	
	function setCharset($type = 'utf-8') {
		$this->_charset = $type;
	}
	function getCharset() {
		return $this->_charset;
	}
	
	function setLanguage($lang = "en-gb") {
		$this->language = strtolower($lang);
	}
	function getLanguage() {
		return $this->language;
	}
	
	function setTitle($title) {
		$this->title = $title;
	}
	function getTitle() {
		return $this->title;
	}
	
	function setDescription($description) {
		$this->description = $description;
	}
	function getDescription() {
		return $this->description;
	}
	
	function setGenerator($generator) {
		$this->_generator = $generator;
	}
	function getGenerator() {
		return $this->_generator;
	}
	
	function setKeywords($keywords) {
		$this->_keywords = $keywords;
	}
	function getKeywords() {
		return $this->_keywords;
	}
	function getHeadData()
	{
		$data = array();
		$data['title']		= $this->title;
		$data['description']= $this->description;
		$data['link']		= $this->link;
		$data['metaTags']	= $this->_metaTags;
		$data['links']		= $this->_links;
		$data['styleSheets']= $this->_styleSheets;
		$data['style']		= $this->_style;
		$data['scripts']	= $this->_scripts;
		$data['script']		= $this->_script;
		$data['custom']		= $this->_custom;
		$data['language']	= $this->language;
		$data['generator']	= $this->_generator;
		$data['_keywords']	= $this->_keywords;
		return $data;
	}
	/**
	 * 设置文档头部数据
	 *
	 */
	function setHeadData($data)
	{
		$this->title		= (isset($data['title'])) ? $data['title'] : $this->title;
		$this->description	= (isset($data['description'])) ? $data['description'] : $this->description;
		$this->link			= (isset($data['link'])) ? $data['link'] : $this->link;
		$this->_metaTags	= (isset($data['metaTags'])) ? $data['metaTags'] : $this->_metaTags;
		$this->_links		= (isset($data['links'])) ? $data['links'] : $this->_links;
		$this->_styleSheets	= (isset($data['styleSheets'])) ? $data['styleSheets'] : $this->_styleSheets;
		$this->_style		= (isset($data['style'])) ? $data['style'] : $this->_style;
		$this->_scripts		= (isset($data['scripts'])) ? $data['scripts'] : $this->_scripts;
		$this->_script		= (isset($data['script'])) ? $data['script'] : $this->_script;
		$this->_custom		= (isset($data['custom'])) ? $data['custom'] : $this->_custom;
		$this->language		= (isset($data['language'])) ? $data['language'] : $this->language;
		$this->_generator	= (isset($data['generator'])) ? $data['generator'] : $this->_generator;
		$this->_keywords	= (isset($data['keywords'])) ? $data['keywords'] : $this->_keywords;
	}
	
	 /**
	 * 添加icon (favicon)
	 *
	 */
	function addFavicon($href, $type = 'image/x-icon', $relation = 'shortcut icon')
	{
		$href = str_replace( '\\', '/', $href );
		$this->_links[] = '<link href="'.$href.'" rel="'.$relation.'" type="'.$type.'" />';
	}
	/**
	 * 添加自定义代码到头部
	 *
	 */
	function addCustomTag( $html )
	{
		$this->_custom[] = trim( $html );
	}
	/**
	 * 获取meta标签
	 *
	 */
	function getMetaData($name, $http_equiv = false)
	{
		$result = '';
		$name = strtolower($name);
		if($name == 'generator') { 
			$result = $this->getGenerator();
		} elseif($name == 'description') {
			$result = $this->getDescription();
		} elseif($name == 'keywords') {
			$result = $this->getKeywords();
		} else{
			if ($http_equiv == true) {
				$result = @$this->_metaTags['http-equiv'][$name];
			} else {
				$result = @$this->_metaTags['standard'][$name];
			}
		}
		return $result;
	}
	/**
	 * 设置meta标签
	 *
	 */
	function setMetaData($name, $content, $http_equiv = false)
	{
		$name = strtolower($name);
		if($name == 'generator') { 
			$this->setGenerator($content);
		} elseif($name == 'description') {
			$this->setDescription($content);
		} elseif($name == 'keywords') {
			$this->setKeywords($content);
		} else {
			if ($http_equiv == true) {
				$this->_metaTags['http-equiv'][$name] = $content;
			} else {
				$this->_metaTags['standard'][$name] = $content;
			}
		}
	}
	/**
	 * 添加样式文件
	 *
	 */
	function addStyleSheet($url, $type = 'text/css', $media = null, $attribs = array())
	{
		$this->_styleSheets[$url]['mime']		= $type;
		$this->_styleSheets[$url]['media']		= $media;
		$this->_styleSheets[$url]['attribs']	= $attribs;
	}
	 /**
	 * 添加样式
	 *
	 */
	function addStyleDeclaration($content, $type = 'text/css')
	{
		if (!isset($this->_style[strtolower($type)])) {
			$this->_style[strtolower($type)] = $content;
		} else {
			$this->_style[strtolower($type)] .= chr(13).$content;
		}
	}
	/**
	 * 添加脚本文件
	 *
	 */
	function addScript($url, $type="text/javascript") {
		$this->_scripts[$url] = $type;
	}
	/**
	 * 添加脚本
	 *
	 */
	function addScriptDeclaration($content, $type = 'text/javascript')
	{
		if (!isset($this->_script[strtolower($type)])) {
			$this->_script[strtolower($type)] = $content;
		} else {
			$this->_script[strtolower($type)] .= chr(13).$content;
		}
	}	
	function setModifiedDate($date) {
		$this->_mdate = $date;
	}
	function getModifiedDate() {
		return $this->_mdate;
	}
	function render( )
	{
		$SYS = &get_instance();
		$SYS->response->setHeader( 'Expires', gmdate( 'D, d M Y H:i:s', time() + 900 ) . ' GMT' );
		if ($mdate = $this->getModifiedDate()) {
			$SYS->response->setHeader( 'Last-Modified', $mdate /* gmdate( 'D, d M Y H:i:s', time() + 900 ) . ' GMT' */ );
		}
		//header('Cache-Control: no-cache, no-store, max-age=0, must-revalidate');  
		$SYS->response->setHeader( 'Content-Type', $this->_mime .  '; charset=' . $this->_charset);
	}
}
# 本程序是一个开源系统,使用时请你仔细阅读使用协议,商业用途请自觉购买商业授权.
# Copyright (C) 长沙米拓信息技术有限公司 (http://www.metinfo.cn). All rights reserved.
?>